/* =============================== 新增元素 =============================== */
import type { ElementCategoriesConfig } from '../../types/constants'
import {
  createElementConfig,
  ElementSelectorContentSourceType,
  ElementSelectorPosition,
  ElementSelectorType,
} from '../../types/constants'
import { ELEMENT_IDENTIFIER_CONFIG } from '../service/element_identifier/types'

const CONFIG: ElementCategoriesConfig = {
  toc: {
    [ELEMENT_IDENTIFIER_CONFIG.blog.customToc.id]: {
      show: false,
      hide: false,
      selectorType: ElementSelectorType.Id,
      selector: '#side-menu-toc',
      position: ElementSelectorPosition.TopRight,
      notExistsHide: true,
      contentSource: ElementSelectorContentSourceType.Custom,
    },
    [ELEMENT_IDENTIFIER_CONFIG.blog.customTocList.id]: {
      show: false,
      hide: false,
      selectorType: ElementSelectorType.Id,
      selector: '#menu_toc_ol',
      contentSource: ElementSelectorContentSourceType.Custom,
    },
  },
  ad: {
    [ELEMENT_IDENTIFIER_CONFIG.ad.adControl.id]: {
      show: false,
      hide: false,
      selectorType: ElementSelectorType.Id,
      selector: '#ad-control',
      position: ElementSelectorPosition.BottomRight,
      contentSource: ElementSelectorContentSourceType.Custom,
    },
  },
}

export const customElement = {
  getAllSelectors(): any[] {
    return Object.entries(CONFIG).reduce((result, [_category, configData]) => {
      return result.concat(
        Object.entries(configData).map(([selector, config]) =>
          createElementConfig(selector, config),
        ),
      )
    }, [] as ReturnType<typeof createElementConfig>[])
  },

  getSelector(category: keyof ElementCategoriesConfig, name_zh: string): any {
    const categoryConfig = CONFIG[category]
    if (!categoryConfig)
      return undefined

    const configEntry = Object.values(categoryConfig).find(
      config => config.nameZh === name_zh,
    )

    return configEntry
      ? createElementConfig(
          Object.keys(categoryConfig).find(k => categoryConfig[k] === configEntry) || '',
          configEntry,
        )
      : undefined
  },
}
